// Package crons
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2023 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
package crons

import (
	"context"
	"encoding/json"
	"fmt"
	"hotgo/internal/library/cron"
	"hotgo/internal/model/entity"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/client"
	"hotgo/utility/tradeDate"
	"io"
)

func init() {
	cron.Register(StockBlock)
}

// StockBlock 测试任务（无参数）
var StockBlock = &cStockBlock{name: "block"}
var StockBlockUrl = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=RealRankingInfo&st=10&apiv=w26&Type=1&c=ZhiShuRanking&PhoneOSNew=1&DeviceID=20ad85ca-becb-3bed-b3d4-30032a0f5923&Index=0&ZSType=7"

type StockBlockResp struct {
	ErrCode string           `json:"errcode"`
	List    []StockBlockItem `json:"list"`
}
type StockBlockItem []any

type cStockBlock struct {
	name string
}

func (c *cStockBlock) GetName() string {
	return c.name
}

// Execute 执行任务
func (c *cStockBlock) Execute(ctx context.Context, parser *cron.Parser) (err error) {
	date := tradeDate.GetLatestDay()
	httpClient := client.GetClient()
	response, err := httpClient.Get(ctx, fmt.Sprintf("%s", StockBlockUrl))
	if err != nil {
		parser.Logger.Warning(ctx, "cron CloseOrder Execute http.Get err:%+v", err)
		return
	}
	defer response.Body.Close()
	body, err := io.ReadAll(response.Body)
	if err != nil {
		parser.Logger.Warning(ctx, "cron CloseOrder Execute io.ReadAll err:%+v", err)
		return err
	}
	var resp StockBlockResp
	err = json.Unmarshal(body, &resp)
	if err != nil {
		parser.Logger.Warning(ctx, "cron CloseOrder Execute json.Unmarshal err:%+v", err)
		return err
	}
	if resp.ErrCode == "0" {
		for _, emResp := range resp.List {
			// {"list":[["801676","地产链",18610,3.124,1.665,100442200607,1803827463,19801413254,-17997585791,1.855,2659716621632,2.46,1211250000,3102690722944,2067567745,18.1368,15.4164,18610,3.124],["801829","低空经济",8609,3.822,0.472,72672055033,424907334,4036070546,-3611163212,1.664,1082150405280,2.8,193409000,1317499158528,75756932,36.9648,22.0802,8609,3.822],["801001","芯片",6734,2.199,0.483,129375097505,911438340,10505771519,-9594333179,1.143,7380802897536,2.23,335165000,9467814179200,-10688861302,112.854,27.5174,6734,2.199],["801235","化工",5943,1.466,0.679,92413425708,304788725,7940116022,-7635327297,1.056,9072629992032,1.47,522325000,10579681207232,3402700807,19.5476,13.6752,5943,1.466],["801314","ST板块",4750,0.001,1.132,9183545169,11247944,489928897,-478680953,1.174,447701437568,0.65,-3446900,499676264064,0,3.6124,45.5332,4750,0.001],["801071","保险",3322,3.23,0.164,11995587853,1328463377,6415458039,-5086994662,1.99,1649217363153,2.83,1024280000,2396066826955,8720220281,12.1363,10.4061,3322,3.23],["801085","人工智能",2411,1.483,0.517,171769818894,-834065904,9500010481,-10334076385,1.044,8197698871456,1.46,-281546000,9955562133440,4820251483,44.0984,25.7135,2411,1.483],["801660","通信",1498,2.113,0.536,96157471862,231385936,8034696356,-7803310420,1.155,4575116789120,2.19,176199000,7712865814784,12338226043,168.501,26.5237,1498,2.113],["801086","玻璃",1189,1.895,0.199,4275517896,61534245,645421826,-583887581,1.582,318331704192,1.9,10971900,377386275840,1580951857,18.1068,14.4705,1189,1.895],["801169","VR\/AR\/MR",1179,2.234,1.435,38914219564,228929868,1881012285,-1652082417,1.166,1694747357824,2.19,103321000,1951601477120,-910876964,46.7259,27.7473,1179,2.234]],"Time":1716096541,"Count":10,"Day":["2024-05-17"],"Min":"0925","Max":"1500","MinDay":"2019-12-23","Title":["第一季度机构增仓","2024年平均PE","2025年平均PE"],"ttag":0.0011700000000000044,"errcode":"0"}
			code, _ := emResp[0].(string)
			name, _ := emResp[1].(string)
			strength, _ := emResp[2].(float64)
			err := service.SysStockBlock().Edit(ctx, &sysin.StockBlockEditInp{
				StockBlock: entity.StockBlock{
					Code:     code,
					Name:     name,
					Date:     date,
					Strength: strength,
				},
			})
			if err != nil {
				parser.Logger.Warning(ctx, "cron CloseOrder Execute service.SysStockZhASpotEm().Edit err:%+v", err)
				continue
			}
		}
	}
	return
}
